# -*- coding: utf-8 -*-

from utils.dbbase import exc_sql
from datetime import datetime
from web.views.tools import md5hex
from web.views.base import set_cache, get_cache

def userid_by_openid(cop_id, open_id):
    user_id_cache_key = md5hex('userid_by_openid' + open_id)
    cache_data = get_cache(user_id_cache_key)
    if cache_data['errcode'] == 0:
        return cache_data['data']
    data = exc_sql(
        r"SELECT userid FROM qrcode_user WHERE cop_id = '{0}' AND open_id ='{1}';".format(cop_id,open_id),
        fetch_type = 2
    )
    set_cache(user_id_cache_key, {'errcode':0,'user_id':data[0]}, 24 * 60)
    return {'errcode':0,'user_id':data[0]}
